package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.req.base.BaseCommand;
import org.json.JSONObject;

import java.util.HashMap;

/**
 *   点赞 （天天慧，拍拍乐，凑对去模块）删除和添加 异步请求
 *   @author daivd
 */
public class PostPraise extends BaseCommand {

    private static  final String TAG = "BaseCommand";
    public static final String  ATION_DEL = "delete";
    public static final String  ATION_ADD = "add";

    private int catid;
    private String userid;
    private String itemid ;
    private String action = "add"; // "add","delete"

    public PostPraise(int catid, String userid, String itemid, String action) {
        this.catid = catid;
        this.userid = userid;
        this.itemid = itemid;
        this.action  = action;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.POST_PRAISE_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("catid", catid + "");
        paramters.put("userid", userid);
        paramters.put("itemid", itemid);
        paramters.put("action", action);
        paramters.put("info", "1"); //区别详情和用户中心的参数
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "PostPraise.yes()");
        try {
            Log.d(TAG, result.object.toString());
            JSONObject jsonObject = new JSONObject(result.object.toString());
            String resultes= jsonObject.getString("msg");
            Log.d(TAG, resultes.toString());
            DataCenter.resultes = resultes.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.POST_PRAISE_SUCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "PostPraise.no()");
        DataCenter.getInstance().callBacks(DataCenter.flag.POST_PRAISE_FAILED);
    }
}
